package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.basic.framework.core.constant.BaseConstant;
import com.fdb.basic.framework.core.util.DataFileUtil;
import com.fdb.efp.nls.service.dao.BatBrhmDao;
import com.fdb.efp.nls.service.domain.BatBrhm;
import com.fdb.efp.nls.service.facade.BatBrhmService;
import com.fdb.efp.nls.service.vo.BatBrhmVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 机构更新中间表的Service实现类
 * </p>
 * @author yusys
 * @createDate 2018-06-08 10:28:22
 */
@Service("batBrhmService")
public class BatBrhmServiceImpl implements BatBrhmService, FrameworkService {
    
    private static Logger logger = LoggerFactory.getLogger(BatBrhmServiceImpl.class);
    
    @Autowired
    private BatBrhmDao batBrhmDao;
    
    @Override
    public int insertBatBrhm(BatBrhmVO inBatBrhmVo) {
        logger.debug("当前新增数据为：" + inBatBrhmVo.toString());
        int count = 0;
        try {
            BatBrhm batBrhm = new BatBrhm();
            beanCopy(inBatBrhmVo, batBrhm);
            count = batBrhmDao.insertBatBrhm(batBrhm);
        } catch (Exception e) {
            logger.error("新增数据发生异常！", e);
            count = -1;
        }
        logger.debug("当前新增数据条数为：" + count);
        return count;
    }
    
    @Override
    public int deleteByPk(BatBrhmVO inBatBrhmVo) {
        logger.debug("当前删除数据条件为：" + inBatBrhmVo.toString());
        int count = 0;
        try {
            BatBrhm batBrhm = new BatBrhm();
            beanCopy(inBatBrhmVo, batBrhm);
            count = batBrhmDao.deleteAll(batBrhm);
        } catch (Exception e) {
            logger.error("删除数据发生异常！", e);
            count = -1;
        }
        logger.debug("根据条件：" + inBatBrhmVo + "删除的数据条数为" + count);
        return count;
    }
    
    @Override
    public BatBrhmVO queryByPk(BatBrhmVO inBatBrhmVo) {
        logger.debug("当前查询参数信息为：" + inBatBrhmVo);
        try {
            BatBrhm batBrhm = new BatBrhm();
            beanCopy(inBatBrhmVo, batBrhm);
            BatBrhm querybatBrhm = batBrhmDao.queryByPk(batBrhm);
            if (Objects.nonNull(querybatBrhm)) {
                BatBrhmVO outBatBrhmVo = beanCopy(querybatBrhm, new BatBrhmVO());
                logger.debug("当前查询结果为：" + outBatBrhmVo.toString());
                return outBatBrhmVo;
            } else {
                logger.debug("当前查询结果为空！");
            }
        } catch (Exception e) {
            logger.error("查询数据发生异常！", e);
        }
        return null;
    }


	@Override
	public boolean fileDataInsertTable(File file, BatBrhmVO clazz) {
		boolean result = false;
		try {
			List<BatBrhmVO> readTxt = DataFileUtil.readTxt(file,clazz.getClass(),"batBrhm.json","GB2312");
			if(readTxt != null && readTxt.size() > 0) {
			    //删除未使用的机构数据
                Iterator<BatBrhmVO> iterator = readTxt.iterator();
                while (iterator.hasNext()){
                    BatBrhmVO batBrhmVO = iterator.next();
                    if ("0030000000000000001".equals(batBrhmVO.getKey1()) //省级机构
                            || "0030000000000019001".equals(batBrhmVO.getKey1()) //韶关
                            || "0030000000000020001".equals(batBrhmVO.getKey1()) //清远
                            || "08300".equals(batBrhmVO.getProvBrNo()) // 江门
                            || "20400".equals(batBrhmVO.getProvBrNo()) //佛冈
                            || "19700".equals(batBrhmVO.getProvBrNo()) //新丰
                            || "03100".equals(batBrhmVO.getProvBrNo())) //佛山
                    {

                    }
                    else
                        iterator.remove();

                }
				int sum = readTxt.size();
				logger.info("文件中总数据量为:"+sum);
				int batchNum = BaseConstant.batchInsertNum_5000;
				logger.info("分批处理设置每次处理条数:"+batchNum);
				int cycleNum = sum % batchNum == 0 ? sum/batchNum : sum/batchNum +1;
				logger.info("需要执行的处理次数:" + cycleNum);
				int insertNum = 0;
                batBrhmDao.deleteAll(new BatBrhm());
				for(int i=0;i<cycleNum;i++) {
					logger.info("开始执行第"+(i+1)+"次处理");
					int start = i*batchNum; 
					int end = start + batchNum;
					logger.info("查询数据范围:"+(start+1)+"~"+end);
                    List<BatBrhmVO> subList = new ArrayList<BatBrhmVO>();
					if(end > readTxt.size()) {
						subList.addAll( readTxt.subList(start, readTxt.size()));
					}else {
						subList.addAll(readTxt.subList(start, end));
					}
					for (BatBrhmVO vo : subList) {
                        //机构状态修改 原来  1 生效  2  失效
					    //现在1	撤并	STD_ZB_ORG_STATUS  0   开办  机构状态
					    if ("1".equals(vo.getBranchStatus())){
                            vo.setBranchStatus("0");
                        }
                        else if ("2".equals(vo.getBranchStatus())){
                            vo.setBranchStatus("1");
                        }
					}
                    int num = batBrhmDao.insertBatBrhmAll(subList);
                    insertNum += num;
				}
				logger.info("文件中数据成功插入数量为:" + insertNum);
				result = true;
			}
		} catch (Exception e) {
			logger.error("插入中间表bat_brhm出错!", e);
		}
		return result;
	}
}